package com.tql.mall.boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tql.mall.boss.mapper.CategoryOneMapper;
import com.tql.mall.boss.service.CategoryOneService;
import com.tql.mall.boss.service.CategoryTwoService;
import com.tql.mall.model.enums.category.OperFailEnum;
import com.tql.mall.model.product.BaseCategory1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class CategoryOneServiceImpl extends ServiceImpl<CategoryOneMapper, BaseCategory1> implements CategoryOneService {


    @Autowired
    private CategoryTwoService categoryTwoService;

    @Override
    public String saveLevelOne(BaseCategory1 baseCategory1) {
        if (StringUtils.isEmpty(baseCategory1.getName())) return OperFailEnum.ADD_FAIL_NAME_NULL.getMsg();
        baseMapper.insert(baseCategory1);
        return null;
    }

    @Override
    public boolean isExist(Long category1Id) {

        BaseCategory1 baseCategory1 = baseMapper.selectById(category1Id);
        if (baseCategory1 == null) return false;
        return true;
    }

    @Override
    public String deleteById(Long id) {
        BaseCategory1 baseCategory1 = baseMapper.selectById(id);
        if (baseCategory1 == null) return OperFailEnum.DELETE_FAIL_ID_NULL.getMsg();
        boolean isRely = categoryTwoService.isExistRely(id);
        if (isRely) return OperFailEnum.DELETE_FAIL_DOWNID_NONULL.getMsg();
        //执行删除
        baseMapper.deleteById(id);
        return null;
    }

//    @Override
//    @MallCache(prefix = RedisConstant.BOSS_CATEGORY)
//    public List<FirstLevel> getAllClassifyInfo() {
//        //查询所有的一级分类
//        QueryWrapper<BaseCategory1> queryWrapper = new QueryWrapper<>();
//        List<BaseCategory1> list = baseMapper.selectList(queryWrapper);
//        List<FirstLevel> resList = new ArrayList<>();
//        BaseCategory1 baseCategory1 = null;
//        for (int i = 0; i < list.size(); i++){
//            baseCategory1 = list.get(i);
//            FirstLevel tmpFirstLevel = new FirstLevel();
//            tmpFirstLevel.setIndex(i + 1);
//            tmpFirstLevel.setCategoryName(baseCategory1.getName());
//            tmpFirstLevel.setCategoryId(baseCategory1.getId());
//            tmpFirstLevel.setCategoryChild(categoryTwoService.getBaseLevelList(baseCategory1.getId()));
//            resList.add(tmpFirstLevel);
//        }
//        return resList;
//    }

    @Override
    public String updateCategoryName(Long id, String newName) {
        //先根据id查询 分类
        BaseCategory1 baseCategory1 = baseMapper.selectById(id);
        //名字相同，则不执行修改
        if (baseCategory1.getName().equals(newName)){
            return null;
        }
        //查询一级分类中是否有和newName重名的分类
        QueryWrapper<BaseCategory1> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",newName);
        BaseCategory1 reNamedCategory1 = baseMapper.selectOne(queryWrapper);
        if (reNamedCategory1 != null){//重名了
            return OperFailEnum.EDIT_FILE_NAME_DEPLICATED.getMsg();
        }
        //执行修改
        baseCategory1.setName(newName);
        baseCategory1.setUpdateTime(new Date());
        baseMapper.updateById(baseCategory1);
        return null;
    }

    @Override
    public List<BaseCategory1> getAllCategory1List() {
        //无查询条件地获得一级分类的list
        return baseMapper.selectList(new QueryWrapper<BaseCategory1>());
    }
}
